package com.lyl.xxx.modules.framework.security.service.impl;

import cn.hutool.core.lang.Assert;
import com.lyl.xxx.modules.framework.security.model.details.LoginDetails;
import com.lyl.xxx.modules.system.account.entity.SysUser;
import com.lyl.xxx.modules.system.account.service.SysMenuService;
import com.lyl.xxx.modules.system.account.service.SysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @author Mr.Luo
 * @date 2022-09-19 11:17
 * @desc
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {
    private final SysUserService sysUserService;
    private final SysMenuService sysMenuService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.getByUsername(username);
        Assert.notNull(sysUser, () -> new UsernameNotFoundException("找不到对应的用户名"));
        Set<String> perms = sysMenuService.getPermsByUserId(sysUser.getUserId());
        return new LoginDetails(sysUser, perms);
    }
}
